import java.io.File;
import java.util.Iterator;

public class ProblemIterator implements Iterator<ApeProblem>
{
	File path;
	File[] problems;
	int curProb = 0;

	public ProblemIterator(String path)
	{
		this.path = new File(path);

		problems = SearchDir.getDirs(this.path);
	}

	public boolean hasNext()
	{
		if (curProb < problems.length)
			return true;
		return false;
	}

	public ApeProblem next()
	{
		File prob = problems[curProb++];
		ApeProblem ape = new ApeProblem(prob);

		ape.title = prob.getName();

		File[] files = SearchDir.getFiles(prob);

		if (findFile("testinput", files) != null)
		{
			ape.file[ApeProblem.PINPUT] = findFile("providedinput", files);
			ape.file[ApeProblem.POUTPUT] = findFile("providedoutput", files);
			ape.file[ApeProblem.TINPUT] = findFile("testinput", files);
			ape.file[ApeProblem.TOUTPUT] = findFile("testoutput", files);
		} else
		{
			ape.file[ApeProblem.PINPUT] = findFile("input", files);
			ape.file[ApeProblem.POUTPUT] = findFile("output", files);
			ape.file[ApeProblem.TINPUT] = findFile("input", files);
			ape.file[ApeProblem.TOUTPUT] = findFile("output", files);
		}

		ape.file[ApeProblem.CRITERIA] = findFile("criteria", files);
		ape.file[ApeProblem.INSTR] = findFile("instr", files);

		return ape;
	}

	public void remove()
	{
	}

	private File findFile(String name, File[] list)
	{
		for (int i = 0; i < list.length; i++)
		{
			if (list[i].getName().toLowerCase().contains(name.toLowerCase()))
				return list[i];
		}
		return null;
	}

}
